package com.kawins.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.EnumSet;

/**
 * @author rankai
 *         createTime 2018-01-2018/1/25 18:11
 */
@Order(value = 5)
public abstract class SpringWebAppInitializer implements WebApplicationInitializer {

    private static final Logger log = LoggerFactory.getLogger(SpringWebAppInitializer.class);

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        log.debug("start loading com.kawins.spring.SpringWebAppInitializer");

        servletContext.addListener(RequestContextListener.class);
        servletContext.addListener(ContextLoaderListener.class);

        //编码器>>必须放在其他过滤器之前
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        FilterRegistration.Dynamic characterEncoding = servletContext
                .addFilter("characterEncodingFilter", characterEncodingFilter);
        characterEncoding.setAsyncSupported(true);
        characterEncoding.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");

        //shiro>>必须放在编码器之后其他过滤器是前
        DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
        delegatingFilterProxy.setTargetFilterLifecycle(true);
        FilterRegistration.Dynamic shiroFilter = servletContext.addFilter("shiroFilter", delegatingFilterProxy);
        shiroFilter.setAsyncSupported(true);
        shiroFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");

        //REST涉及的过滤器
        HttpPutFormContentFilter httpPutFormContentFilter = new HttpPutFormContentFilter();
        FilterRegistration.Dynamic httpPutFormContent = servletContext
                .addFilter("httpPutFormContentFilter", httpPutFormContentFilter);
        httpPutFormContent.setAsyncSupported(true);
        httpPutFormContent.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");

        //获取put表单中的参数值
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        FilterRegistration.Dynamic hiddenHttpMethod = servletContext
                .addFilter("hiddenHttpMethodFilter", hiddenHttpMethodFilter);
        hiddenHttpMethod.setAsyncSupported(true);
        hiddenHttpMethod.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    }
}
